<?php
/**
 * class App_Controller_User
 *
 * @author kim
 */
class App_Controller_User extends Zend_Controller_Action
{
    /**
     * default action
     */
    public function indexAction()
    {
        $this->_helper->redirector('index', 'index');
    }

    /**
     * user login action
     *
     * @return null
     */
    public function loginAction()
    {
        if (App_Model_User::isLogedIn()) {
            $this->_helper->redirector('index', 'index');
        }

        // login form object
        $form = new App_Form_User_Login();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            // validate post data
            if ($form->isValid($data)) {
                // process user login
                $user = new App_Model_User();
                $result = $user->getLoginResult($form->getValues());

                if (true === $result) {
                    $this->_helper->redirector('index', 'index');
                } else {
                    $this->view->errorMsg = $result;
                }
            } else {
                $form->populate($data);
            }
        }

        $this->view->formLogin = $form;
    }

    /**
     * user logout action
     * redirect to login when user logout
     *
     * @return null
     */
    public function logoutAction()
    {
        $user = new App_Model_User();
        $user->logout();
        $this->_helper->redirector('login');
    }

    /**
     * user register action
     *
     * @return null
     */
    public function registerAction()
    {
        // login form object
        $form = new App_Form_User_Register();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            // validate post data
            if ($form->isValid($data)) {
                // process user register
                $user = new App_Model_User();
                $result = $user->getRegisterResult($form->getValues());
                if (true === $result) {
                    if (App_Model_User::isLogedIn()) {
                        $this->_helper->redirector('index', 'index');
                    } else {
                        $this->_helper->redirector('login');
                    }
                }
            } else {
                $form->populate($data);
            }
        }

        $this->view->formRegister = $form;
    }

    /**
     * reset password if user forgot the password
     *
     * @return null
     */
    public function resetPwdAction()
    {
        // login form object
        $form = new App_Form_User_ResetPwd();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            // validate post data
            if ($form->isValid($data)) {

                // try to set the key for password reset
                $email = $form->getValue('email');
                $key = Zend_Crypt::hash('md5', time());
                $user = new App_Model_User();
                $result = $user->setPasswordKey($email, $key);

                if (true === $result) {
                    // try to send the reset password email
                    $mail = new App_Model_Mail();
                    $url = $this->view->serverUrl()
                         . $this->_helper->url('reset-pwd-confirm', 'user', null, array('email' => $email, 'key' => $key));
                    $mail->send($email, 'Reset your password', '<a href="' . $url . '">' . $url . '</a>');
                    $this->view->errorMsg = 'Password reset email sent.';
                } else {
                    $this->view->errorMsg = 'Invalid email address.';
                }
            } else {
                $form->populate($data);
            }
        }

        $this->view->formResetPwd = $form;
    }

    /**
     * reset password confirm
     *
     * @return null
     */
    public function resetPwdConfirmAction()
    {
        $email = $this->getRequest()->getParam('email');
        $key = $this->getRequest()->getParam('key');

        $form = new App_Form_User_ResetPwdConfirm();
        $form->email->setValue($email);
        $form->key->setValue($key);

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            // validate post data
            if ($form->isValid($data)) {
                $user = new App_Model_User();
                $result = $user->getResetPasswordResult($email, $key, $form->getValue('password_new'));
                if (true === $result) {
                    $this->view->errorMsg = 'Password reseted.';
                } else {
                    $this->view->errorMsg = 'Fail to reset password.';
                }
            } else {
                $form->populate($data);
            }
        }

        $this->view->formResetPwdConfirm = $form;
    }

    /**
     * user account management
     *
     * @todo
     * 1. user account management
     * 2. pass the return url then the action can return to account after user login
     */
    public function accountAction()
    {
        if (false == App_Model_User::isLogedIn()) {
            $this->_helper->redirector('login');
        }
    }

}
